package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.domain.AppUser;
import com.bezdelnick.forum.domain.UserRole;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.ModeratorsList;
import com.bezdelnick.forum.web.page.template.BaseTemplate;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 24.08.12
 * Time: 0:53
 * To change this template use File | Settings | File Templates.
 */
@AuthorizeInstantiation("ROLE_SUPER")
public class SuperAdminPage extends BaseTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(SuperAdminPage.class);

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    private List<AppUser> appUserList;

    public SuperAdminPage() {
        this(new PageParameters());
    }

    public SuperAdminPage(PageParameters parameters) {
        this.getPageParameters().mergeWith(parameters);
        if (!this.getPageParameters().get("action").isNull() && !this.getPageParameters().get("id").isNull()) {
            String action = this.getPageParameters().get("action").toString();

            if (action.equals("activation")) {
                long userId = this.getPageParameters().get("id").toLong();

                List<UserRole> userRoles = forumService.getRoleDao().loadRolesByUserId(userId);
                boolean haveModerAuthority = false;
                for (UserRole role : userRoles) {
                    if (role.getAuthority().equals(forumService.getAuthorityModer())) {
                        haveModerAuthority = true;
                        break;
                    }
                }

                /* If the user is an administrator */
                if (haveModerAuthority) {
                    AppUser appUser = forumService.getAppUserDao().getUserById(userId);
                    /* If the administrator has not activated */
                    if (!appUser.isEnabled()) {
                        appUser.setEnabled(true);
                        if (forumService.getAppUserDao().updateUser(appUser)) {
                            this.getPageParameters().remove("action");
                            this.getPageParameters().remove("id");
                            LOGGER.info("Administrator has been activated.");
                        }
                    } else {
                        LOGGER.info("Administrator is activated already.");
                    }
                } else {
                    LOGGER.info("User is not administrator.");
                }
            } else if (action.equals("remove")) {
                long userId = this.getPageParameters().get("id").toLong();
                //AppUser currentModerator = forumService.getAppUserDao().getUserById(userId);
                forumService.getRoleDao().deleteRolesByUserId(userId);
                forumService.getAppUserDao().deleteUser(userId);
            }
        }

        this.appUserList = forumService.getAppUserDao().loadModeratorsWithoutEnabled();

        ModeratorsList moderatorsList = new ModeratorsList("list_admins", appUserList);
        add(moderatorsList);
    }
}
